$(function() {
    var bdbInnerDomainList = {
        /* 初始数据 */
        defData: {
            pageSize: 20 // 每页分页数量.
        },

        /* 页面状态 */
        pageState: {
            search: '', // 搜索框值.
            page: 1, // 分页值.
            noData: 'def' // search: 没有匹配的结果; def: 暂无记录.
        },
        resetPageState: function() {
            this.pageState = {
                search: '',
                page: 1,
                noData: 'def'
            };

            this.list();
        },

        /* 添加 */
        addEvent: function() {
            var _this = this,
                formVerify;

            // 第一步表单验证.
            formVerify = $('#bdb-innerdomain-add-form').validate({
                // onkeyup: false,
                errorElement: "div",
                errorPlacement: function (error, element) {
                    error.addClass("help-block");
                    if (element.prop("type") === "checkbox" || element.prop("type") === "radio") {
                        error.appendTo(element.parents(".bdb-vc"));
                    } else {
                        error.insertAfter(element);
                    }
                },
                submitHandler: function(form, e) {
                    e.preventDefault();
                    _this.addSave(form);
                }
            });

            // 点击添加响应事件.
            $('#bdb-innerdomain-add').off('click').on('click', function() {
                _this.addDefDataProc(formVerify); // 默认数据处理.
            });
        },
        addDefDataProc: function(formVerify) {
            var _this = this;
            bdbMain.mLRManag({mask: true, loading: true});
            $.post('/InnerDomain/innerDomainInit', function(response) {
                if (response.code == 0) {
                    var dialogEle = $('#bdb-innerdomain-add-dialog');
                    if (response.data.domainSuffix) {
                        dialogEle.find('#masterDomainKey-layer-add').val(response.data.domainSuffix.ser);
                    }
                    bdbMain.mLSleep({loading: false}, function() {
                        $(".bdbBlankEle").bdbDialog({
                            ele: '#bdb-innerdomain-add-dialog',
                            formObj: formVerify,
                            form: dialogEle.find('#bdb-innerdomain-add-form'),
                            verifyResetInfo: true
                        });
                    });
                } else {
                    bdbMain.mLSleep({mask: false, loading: false, alert: {type: 2, msg: response.msg}});
                }
            }, 'json');
        },
        addSave: function(form) {
            var _this = this,
                form = $(form);

            // 请求已发出.
            if (!bdbMain.ajaxInfo.state) {
                bdbMain.mLRManag({alert: {type: 2, msg: bdbMain.ajaxInfo.text}});
                return ;
            }

            // 发送请求.
            bdbMain.ajaxInfo.state = 0; // 加锁.
            bdbMain.mLRManag({mask: true, maskUp: 101, loading: true});
            $.post('/InnerDomain/addInnerDomain', form.serialize(), function(response) {
                if (response.code == 0) {
                    bdbMain.mLSleep({maskDown: 99, loading: false, alert: {type: 1, msg: '添加成功.'}}, function() {
                        $('#bdb-innerdomain-add-cancel').click();
                        bdbMain.mLSleep({}, function() {
                            _this.resetPageState();
                        });
                    });
                } else {
                    bdbMain.mLSleep({maskDown: 99, loading: false, alert: {type: 2, msg: response.msg}});
                }

                bdbMain.ajaxInfo.state = 1; // 释放锁.
            });
        },

        /* 搜索 */
        searchEvent: function() {
            var _this = this,
                searchInputEle = $('.bdb-search-input');

            searchInputEle.off('focusin').on('focusin', '.search-text', function(e) {
                var _cThis = $(this),
                    _btnEle = $(this).parent().find('span');

                if (!_btnEle.hasClass('search-clear')) {
                    _cThis.animate({width: '240px'}, 500);
                    _btnEle.animate({left: '240px'}, 500, function() {
                        _btnEle.removeClass('search-gray').addClass('search-blue');
                    });
                }
            });

            searchInputEle.off('click').on('click', 'span', function(e) {
                var left = parseInt($(this).css('left'));
                if ($(this).hasClass('search-blue') && left > 0) {
                    _this.pageState.search = searchInputEle.find('.search-text').val();
                    _this.pageState.noData = 'search';
                    $(this).removeClass('search-blue').addClass('search-clear');
                    _this.list();
                } else if (left > 0) {
                    _this.pageState.search = '';
                    _this.pageState.noData = 'def';
                    searchInputEle.find('.search-text').val('');
                    $(this).removeClass('search-clear').addClass('search-blue');
                    _this.list();
                }
            });
        },

        searchProc: function() {

        },

        /* 列表 */
        listEvent: function() {
            var _this = this;
            $('.bdb-list').off('click.listEvent').on('click.listEvent', function(e) {
                var ele = $(e.target),
                    id = ele.parents('.bdb-list-row').attr('data-id');

                if (ele.hasClass('js-bdb-innerdomain-del')) { // 删除.
                    _this.listDel(id);
                }
            });
        },
        listDel: function(id) {
            var _this = this;

            bdbMain.confirm({
                textInfo: {
                    content: '确定要删除这条记录？',
                    btn: {}
                },
                // note: true,
                params: {
                    url: '/InnerDomain/deleteInnerDomain',
                    data: {
                        id: id
                    }
                },
                callback: function(ele, params) {
                    _this.listDelCallbackProc(ele, params);
                }
            });
        },
        listDelCallbackProc: function(ele, params) {
            var _this = this;
            ele.hide();

            // 请求已发出.
            if (!bdbMain.ajaxInfo.state) {
                bdbMain.mLRManag({alert: {type: 2, msg: bdbMain.ajaxInfo.text}});
                return ;
            }

            // 发送请求.
            bdbMain.ajaxInfo.state = 0; // 加锁.
            bdbMain.mLRManag({mask: true, loading: true});
            $.post(params.url, {
                innerDomainId: params.data.id
            }, function(response) {
                if (response.code == 0) {
                    bdbMain.mLSleep({mask: false, loading: false, alert: {type: 1, msg: '删除成功'}}, function() {
                        bdbMain.mLSleep({}, function() {
                            _this.list({page: bdbMain.pageCheck(_this.pageState.page)});
                        });
                    });
                } else {
                    bdbMain.mLSleep({mask: false, loading: false, alert: {type: 2, msg: response.msg}});
                }

                bdbMain.ajaxInfo.state = 1; // 释放锁.
            });
        },
        listProc: function(data) {
            var tpl = '';
            $.each(data.domainList, function(i, n) {
                tpl += '<div class="bdb-list-row" data-id="' + n.inner_domain_id + '">\
                            <div class="bdb-mx-w-col-1 bdb-ellipsis">' + n.domain_name + '</div>\
                            <div class="bdb-mx-w-col-2 bdb-ellipsis">' + n.domain_suffix + '</div>\
                            <div class="bdb-mx-w-col-3 bdb-ellipsis">' + n.admin_real_name + '</div>\
                            <div class="bdb-mx-w-col-4 bdb-ellipsis">' + n.create_time + '</div>\
                            <div class="bdb-mx-w-col-5 bdb-ellipsis">' + (n.update_time != 0 ? n.update_time : "-") + '</div>\
                            <div class="bdb-mx-w-col-6">\
                                <div class="bdb-option-btn bdb-p-r bdb-mgn-t-10">\
                                    <a href="/InnerDomain/innerDomainRecordList/' + n.inner_domain_id + '">配置</a>\
                                    <div class="v-line-ffa007"></div>\
                                    <a class="bdb-f-c-c8baac bdb-mgn-l--1 js-bdb-innerdomain-del" href="javascript:void(0);">删除</a>\
                                </div>\
                            </div>\
                        </div>';
            });

            return tpl;
        },
        list: function(obj) {
            var _this = this,
                obj = obj || {page: 1};

            _this.pageState.page = obj.page;
            bdbMain.mLRManag({mask: true, loading: true});

            // 获取列表.
            $.post('/InnerDomain/innerDomainList', {
                pageSize: _this.defData.pageSize,
                pageNumber: _this.pageState.page,
                domainName: _this.pageState.search
            }, function(response) {
                var totalRowCnt = 0,
                    listHtml = '';

                if (response.code == 0) {
                    totalRowCnt = parseInt(response.data.totalCount) > 0 ? parseInt(response.data.totalCount) : 0;
                    listHtml = _this.listProc(response.data);
                    _this.listNull(totalRowCnt);
                    $('.bdb-list').html(listHtml);
                    bdbMain.mLSleep({mask: false, loading: false});

                    // 分页插件.
                    $("#js-bdb-creatPage").bdbPagination({
                        totalRowCnt: totalRowCnt,
                        currentPage: _this.pageState.page,
                        pageSize: _this.defData.pageSize
                    });
                    $('.bdb-record-cnt').html(bdbMain.pageInfo.recordCnt.replace('{cnt}', totalRowCnt));
                } else {
                    bdbMain.mLSleep({mask: false, loading: false, alert: {type: 2, msg: response.msg}});
                }
			    $('.check-all-icon').removeClass('checkAll').addClass('checkNone'); // 撤销全选.
            });
        },
        listNull: function(totalRowCnt) {
            var noResult = $('.bdb-no-result'),
                noSearchRecord = $('.bdb-no-search-record');

            if (totalRowCnt <= 0) {
                if (this.pageState.noData == 'search') { // 查询.
                    noResult.hide();
                    noSearchRecord.show();
                } else { // 暂无记录.
                    noResult.show();
                    noSearchRecord.hide();
                }
            } else {
                noResult.hide();
                noSearchRecord.hide();
            }
        },

        /* 分页 */
        pageEvent: function() {
            var　_this = this;

            /* 页码事件 */
            $('.bdb-creatPage').off('click').on('click', '.bdb-pagination', function(e) {
                e.preventDefault();
                var ele = $(e.target),
                    page = parseInt(ele.text());

                if (!isNaN(page)) {
                    _this.list({page: page});
                }
            });
        },

        /* 屏幕高度适应 */
        contentSizeResize: function () {
            $(window).resize(function() {
                $('.bdb-list').bdbScreenH(1);
            });
        },

        /* 初始化 */
        init: function() {
            var _this = this;

            _this.addEvent(); // 添加.
            _this.list(); // 加载列表.
            _this.listEvent(); // 列表操作事件.
            _this.pageEvent(); // 分页事件.
            _this.searchEvent(); // 搜索.
			_this.contentSizeResize(); //内容区域屏幕适应.

            $('.bdb-list').bdbScreenH(1); // 右侧列表区域自适应.
        }
    };

    bdbInnerDomainList.init();
});
